<template>
  <div class="zone-manage-page">
    <el-card style="max-width: 1200px; margin: 0 auto;">
      <div class="toolbar">
        <el-button @click="$router.back()">返回</el-button>
        <el-button type="primary" @click="onAdd">新增分区</el-button>
      </div>
      <el-form v-if="formVisible" :model="form" label-width="90px" style="max-width: 400px; margin-bottom: 24px;">
        <el-form-item label="所属仓库" required>
          <el-select v-model="form.warehouseId" style="width: 180px">
            <el-option v-for="w in warehouseList" :key="w.id" :label="w.name" :value="w.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="分区名称" required>
          <el-input v-model="form.name" maxlength="10" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.desc" maxlength="30" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.sort" :min="1" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave">保存</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="zoneList" border size="small" style="margin-bottom: 0;">
        <el-table-column label="分区名称">
          <template #default="scope">
            <span>{{ getWarehouseName(scope.row.warehouseId) }} - {{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="描述" />
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <div style="display: flex; gap: 8px; justify-content: flex-start; padding-left: 8px;">
              <el-button size="small" @click="onEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
// 假设locationList为全局/父组件/Pinia数据，这里用mock
const locationList = ref([
  { id: 1, zone: 1, warehouse: 1, stock: 60 },
  { id: 2, zone: 1, warehouse: 1, stock: 80 },
  { id: 3, zone: 2, warehouse: 2, stock: 30 }
])
function canDeleteZone(zoneId: number, warehouseId?: number) {
  return !locationList.value.some(l => l.zone === zoneId && l.warehouse === warehouseId && l.stock > 0)
}
const router = useRouter()
const warehouseList = ref([
  { id: 1, name: '上海仓' },
  { id: 2, name: '北京仓' }
])
const zoneList = ref([
  { id: 1, warehouseId: 1, name: 'A区', desc: '主分区', sort: 1 },
  { id: 2, warehouseId: 1, name: 'B区', desc: '次分区', sort: 2 },
  { id: 3, warehouseId: 2, name: 'C区', desc: '北京分区', sort: 1 }
])
const formVisible = ref(false)
const isEdit = ref(false)
const form = ref({ id: 0, warehouseId: 0, name: '', desc: '', sort: 1 })
function getWarehouseName(id: number) {
  return warehouseList.value.find(w => w.id === id)?.name || ''
}
function onAdd() {
  form.value = { id: 0, warehouseId: warehouseList.value[0]?.id || 0, name: '', desc: '', sort: 1 }
  isEdit.value = false
  formVisible.value = true
}
function onEdit(row: any) {
  form.value = { ...row }
  isEdit.value = true
  formVisible.value = true
}
function onSave() {
  if (!form.value.name) return ElMessage.warning('请输入分区名称')
  if (!form.value.warehouseId) return ElMessage.warning('请选择所属仓库')
  // 校验同一仓库下分区名称不能重复
  const isDuplicate = zoneList.value.some(z => z.name === form.value.name && z.warehouseId === form.value.warehouseId && (isEdit.value ? z.id !== form.value.id : true))
  if (isDuplicate) return ElMessage.error('同一仓库下分区名称已存在，不允许重名！')
  if (isEdit.value) {
    const idx = zoneList.value.findIndex(t => t.id === form.value.id)
    if (idx > -1) zoneList.value[idx] = { ...form.value }
    ElMessage.success('分区修改成功')
  } else {
    zoneList.value.push({
      id: Date.now(),
      warehouseId: form.value.warehouseId,
      name: form.value.name,
      desc: form.value.desc,
      sort: form.value.sort
    })
    ElMessage.success('分区新增成功')
  }
  formVisible.value = false
}
function onCancel() {
  formVisible.value = false
}
function onDelete(row: any) {
  if (!canDeleteZone(row.id, row.warehouseId)) {
    ElMessage.error('该分区下存在商品库存，无法删除！')
    return
  }
  ElMessageBox.confirm('确定要删除该分区吗？', '提示', { type: 'warning' })
    .then(() => {
      zoneList.value = zoneList.value.filter(t => t.id !== row.id)
      ElMessage.success('删除成功')
    })
}
</script>

<style scoped>
.zone-manage-page { padding: 24px; background: #f7f8fa; min-height: 100vh; }
.toolbar { display: flex; gap: 12px; margin-bottom: 24px; align-items: center; }
</style> 